package zhang.lao.service.console;/**
 * Created by zhangqin on 2017/9/18.
 */

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhang.lao.build.mybatis.jdbc.auto.dao.WnMachineDeviceLinkMapper;
import zhang.lao.build.mybatis.jdbc.auto.dao.WnMachineToolLinkMapper;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.dao.base.*;
import zhang.lao.pojo.console.req.WnMachineDeviceLinkReq;
import zhang.lao.pojo.console.req.WnMachineLinkQuery;
import zhang.lao.pojo.console.req.WnMachineToolLinkReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.WnMacheineDeviceImeiResp;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @create 2017-09-18 14:06
 **/

@Service
public class WnMachineLinkService {
    @Resource
    private WnMachineToolDao wnMachineToolDao;
    @Resource
    private WnMachineDeviceDao wnMachineDeviceDao;
    @Resource
    private WnMachineDeviceImeiDao wnMachineDeviceImeiDao;
    @Resource
    private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
    @Resource
    private WnMachineToolLinkDao wnMachineToolLinkDao;
    public BootStrapGridResp getMachineToolList(WnMachineLinkQuery wnMachineLinkQuery){
        List<WnMachineTool> wnMachineTools = wnMachineToolDao.selectByMachineId(wnMachineLinkQuery);
        return new BootStrapGridResp(wnMachineTools.size(),wnMachineTools);
    }

    public BootStrapGridResp getMachinegt06List(WnMachineLinkQuery wnMachineLinkQuery){
        wnMachineLinkQuery.setDeviceType("gt06");
        List<WnMacheineDeviceImeiResp> wnMachineTools = wnMachineDeviceImeiDao.findWnMacheineDeviceImeiByMachineId(wnMachineLinkQuery);

        return new BootStrapGridResp(wnMachineTools.size(),wnMachineTools);

    }

    public BootStrapGridResp getMachineAutoList(WnMachineLinkQuery wnMachineLinkQuery){
        wnMachineLinkQuery.setDeviceType("windows_auto_machine");
        List<WnMachineDevice> wnMachineTools = wnMachineDeviceDao.findListByMachineId(wnMachineLinkQuery);

        return new BootStrapGridResp(wnMachineTools.size(),wnMachineTools);
    }


    @Transactional
    public HttpResult machinegt06Link(WnMachineDeviceLinkReq wnMachineDeviceLink){
        WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
        wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(Integer.parseInt(wnMachineDeviceLink.getDeviceId()));
        wnMachineDeviceLinkDao.deleteByExample(wnMachineDeviceLinkExample);
        WnMachineDeviceLink wnMachineDeviceLink1 = new WnMachineDeviceLink();
        wnMachineDeviceLink1.setDeviceId(Integer.parseInt(wnMachineDeviceLink.getDeviceId()));
        wnMachineDeviceLink1.setMachineId(wnMachineDeviceLink.getMachineId());
        wnMachineDeviceLink1.setId(UUIDTool.getUUID());
        wnMachineDeviceLinkDao.insertSelective(wnMachineDeviceLink1);
        return HttpResultUtil.buildSuccess();

    }
    @Transactional
    public HttpResult machineAutoLink(WnMachineDeviceLinkReq wnMachineDeviceLink){
        WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
        wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(Integer.parseInt(wnMachineDeviceLink.getDeviceId()));
        wnMachineDeviceLinkDao.deleteByExample(wnMachineDeviceLinkExample);
        WnMachineDeviceLink wnMachineDeviceLink1 = new WnMachineDeviceLink();
        wnMachineDeviceLink1.setDeviceId(Integer.parseInt(wnMachineDeviceLink.getDeviceId()));
        wnMachineDeviceLink1.setMachineId(wnMachineDeviceLink.getMachineId());
        wnMachineDeviceLink1.setId(UUIDTool.getUUID());
        wnMachineDeviceLinkDao.insertSelective(wnMachineDeviceLink1);
        return HttpResultUtil.buildSuccess();

    }
    @Transactional
    public HttpResult machineToolLink(WnMachineToolLinkReq wnMachineToolLink){
        WnMachineToolLinkExample wnMachineToolLinkExample = new WnMachineToolLinkExample();
        wnMachineToolLinkExample.createCriteria().andMachineToolIdEqualTo(wnMachineToolLink.getToolId());
        wnMachineToolLinkDao.deleteByExample(wnMachineToolLinkExample);
        WnMachineToolLink wnMachineToolLink1 = new WnMachineToolLink();
        wnMachineToolLink1.setMachineToolId( wnMachineToolLink.getToolId());
        wnMachineToolLink1.setMachineId(wnMachineToolLink.getMachineId());
        wnMachineToolLink1.setId(UUIDTool.getUUID());
        wnMachineToolLinkDao.insertSelective(wnMachineToolLink1);
        return HttpResultUtil.buildSuccess();

    }
}
